package com.zzy.ecommerce.web;

import com.zzy.ecommerce.domain.*;
import com.zzy.ecommerce.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PageController {
    @Autowired
    private AccountServiceImpl accountService;
    @Autowired
    private GoodsServiceImpl goodsService;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private EarnServiceImpl earnService;
    @Autowired
    private UserServiceImpl userService;
    @GetMapping("/")
    public String index(Model model){
        return "index";
    }

    @GetMapping("/user/userorderpage")
    public String userorderpage(Integer id,Model model,HttpSession session){
        List<Goods> goodsList = new ArrayList<>();
        Integer userlogin = (Integer) session.getAttribute("userlogin");
        List<TOrder> tOrders=null;
        if (userlogin!=null){
            tOrders = orderService.selectAllUserOrder(userlogin);
            Double userAllOrderPrice = orderService.selectOneUserAllOrderPrice(userlogin);
            model.addAttribute("userAllOrderPrice",userAllOrderPrice);
            model.addAttribute("userlogin",userlogin);
            session.removeAttribute("userlogin");
            System.out.println(userlogin);

        }else {
            System.out.println(id);
            tOrders = orderService.selectAllUserOrder(id);
            Double userAllOrderPrice = orderService.selectOneUserAllOrderPrice(id);
            model.addAttribute("userAllOrderPrice",userAllOrderPrice);
            model.addAttribute("userlogin",id);
        }
        for (TOrder order : tOrders){
            TGoods tGoods = goodsService.selectOneGoodsById(order.getGoodsid());
            Goods goods = new Goods();
            goods.setGoodsid(tGoods.getGoodsid());
            goods.setGoodsname(tGoods.getGoodsname());
            goods.setGoodsprice(tGoods.getGoodsprice());
            goods.setOrderprice(order.getOrdercount()*tGoods.getGoodsprice());
            goods.setOrderid(order.getId());
            goodsList.add(goods);
        }
        String msg = (String) session.getAttribute("msg");
        model.addAttribute("msg",msg);
        model.addAttribute("goods",goodsList);
        return "/user/userorder";
    }

    @PostMapping("/user/deleteuseroneorder")
    public String deleteuseroneorder(Integer id,Model model,HttpSession session){
        //通过id 查找用户id并放入
        TOrder tOrder = orderService.selectOneOrderById(id);
        session.setAttribute("userlogin",tOrder.getUserid());
        Integer integer = orderService.deleteOneOrder(id);
        if (integer!=null){
            session.setAttribute("msg","删除订单成功");
        }else {
            session.setAttribute("msg","删除订单失败");
        }
        return "redirect:/user/userorderpage";
    }

    @GetMapping("/user/useracountpage")
    public String useroccountpage(Integer id,Model model,HttpSession session){
        Integer userlogin = (Integer) session.getAttribute("us");
        TAccount tAccount=null;
        if (userlogin!=null){
            model.addAttribute("userlogin",userlogin);
            tAccount = accountService.selectOneAccount(userlogin);
        }else {
            tAccount = accountService.selectOneAccount(id);
            model.addAttribute("userlogin",id);
        }
        model.addAttribute("account",tAccount);
        String msg = (String) session.getAttribute("msg");
        model.addAttribute("msg",msg);
        return "/user/useracount";
    }

    @PostMapping("/user/insertoneacccount")
    public String insertoneacccount(TAccount account,HttpSession session){
        TUser tUser = userService.selectOneUserByUserName(account.getUsername());
        if (tUser.getPassword().equals(account.getFirstpassword())){
            account.setUserid(tUser.getId());
            System.out.println(account);
            accountService.insertOneAccount(account);
            userService.updateAccount(account.getPassword(),tUser.getId());
            session.setAttribute("us",tUser.getId());
            session.setAttribute("msg","修改成功");
        }else {
            session.setAttribute("msg","修改失败 密码不正确");
        }
        return "redirect:/user/useracountpage";
    }

    @GetMapping("/user/userhomepage")
    public String userhomepage(Model model, HttpSession session){
        List<TGoods> goods = goodsService.selectAllGoods();

        String user = (String) session.getAttribute("user");
        TUser tUser = userService.selectOneUserByUserName(user);

        model.addAttribute("user",user);
        model.addAttribute("goods",goods);
        //添加购物车信息
        String msg1 = (String) session.getAttribute("msg");
        model.addAttribute("msg",msg1);
        //拼接请求
        model.addAttribute("userlogin",tUser.getId());
        return "/user/userhomepage";
    }

    @PostMapping("/user/addoneorder")
    public String addoneorder(Order order, HttpSession session){
        TUser user = userService.selectOneUserByUserName(order.getUsername());
        TGoods goods = goodsService.selectOneGoodsById(order.getGoodsid());
        if (order.getOrdercount()<=goods.getGoodsstock()){
            goodsService.updateOneGoodsStock(goods.getGoodsid(), (goods.getGoodsstock()-order.getOrdercount()));
            TOrder tOrder = new TOrder();
            tOrder.setGoodsid(goods.getGoodsid());
            tOrder.setUserid(user.getId());
            tOrder.setOrdercount(order.getOrdercount());
            tOrder.setOrderprice(goods.getGoodsprice()*order.getOrdercount());
            orderService.insertOneOrder(tOrder);
            session.setAttribute("msg","添加购物车成功");
        }else {
            session.setAttribute("msg","添加购物车失败 库存不足");
        }
        return "redirect:/user/userhomepage";
    }


    @GetMapping("/user/exit")
    public String exit(Model model,HttpSession session){
        session.removeAttribute("login");
        session.removeAttribute("user");
        session.removeAttribute("msg");
        session.removeAttribute("us");
        session.removeAttribute("userlogin");
        return "forward:/";
    }




//===========================================admin
    @GetMapping("/adminlogin")
    public String adminlogin(Model model){

        return "/admin/adminhomepage";
    }

    @PostMapping("/admin/adminaccountpage")
    public String adminaccountpage(Model model){
        List<TGoods> goods = goodsService.selectAllGoods();
        model.addAttribute("goods",goods);
        return "/admin/adminacount";
    }

    @GetMapping("/admin/getalluserpage")
    public String getalluserpage(Model model){
        List<TAccount> Accounts = accountService.selectAllAccount();
        model.addAttribute("accounts",Accounts);
        return "/admin/getalluserpage";
    }

    @GetMapping("/admin/deleteoneuser")
    public String deleteoneuser(Integer userId,Model model){
        Integer integer = accountService.deleteOneAccount(userId);
        if (integer!=null){
            model.addAttribute("msg","删除成功");
            return "forward:/admin/getalluserpage";
        }else {
            model.addAttribute("msg","没有这条数据");
            return "forward:/admin/getalluserpage";

        }
    }
    @GetMapping("/admin/addgoodspage")
    public String addgoodspage(TGoods tGoods,Model model){
        Integer integer = goodsService.insertOneGoods(tGoods);
        if (integer!=null){
            model.addAttribute("add","添加成功");
            return "forward:/admin/selectallgoodspage";
        }else {
            model.addAttribute("add","添加失败");
            return "forward:/admin/selectallgoodspage";
        }
    }

    @GetMapping("/admin/deleteonegoods")
    public String deleteonegoods(Integer id ,Model model){
        Integer integer = goodsService.deleteOneGoods(id);
        if (integer!=null){
            model.addAttribute("msg","删除成功");
            return "forward:/admin/selectallgoodspage";
        }else {
            model.addAttribute("msg","没有这条商品");
            return "forward:/admin/selectallgoodspage";
        }
    }

    @GetMapping("/admin/selectallgoodspage")
    public String selectallgoodspage(Model model){
        List<TGoods> tGoods = goodsService.selectAllGoods();
        model.addAttribute("goods",tGoods);
        return "/admin/addgoodspage";
    }

    @GetMapping("/admin/selectallorderpage")
    public String selectallorderpage(Model model){
        List<Order> orders = orderService.selectAllOrder();
        model.addAttribute("orders",orders);
        return "/admin/allorderpage";
    }

    @GetMapping("/admin/deleteoneorder")
    public String deleteoneorder(Integer id,Model model){
        //查询一条
        TOrder tOrder = orderService.selectOneOrder(id);
        //插入一条
        TEarn earn = new TEarn();
        earn.setGoodsid(tOrder.getGoodsid());
        earn.setUserid(tOrder.getUserid());
        earn.setOrderid(tOrder.getOrdercount());
        earn.setFinalprice(tOrder.getOrderprice());
        earnService.insertOneEarn(earn);
        //删除一条
        Integer integer = orderService.deleteOneOrder(id);
        if (integer!=null){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","没有这条数据");
        }
        return "forward:/admin/selectallorderpage";
    }

    @GetMapping("/admin/selectallearn")
    public String selectallearn(Model model){
        List<Earn> earns = earnService.selectAllEarn();
        model.addAttribute("earns",earns);
        return "/admin/getallearnpage";
    }

    @GetMapping("/admin/deleteoneearn")
    public String deleteoneearn(Integer id ,Model model){
        Integer integer = earnService.deleteOneEarn(id);
        if (integer!=null){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除失败");
        }
        return "forward:/admin/selectallearn";
    }

    @GetMapping("/admin/deleteallgoods")
    public String deleteallgoods(Model model){
        Integer integer = goodsService.deleteAllGoods();
        if (integer!=null){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除失败");
        }
        return "forward:/admin/selectallorderpage";
    }

    @GetMapping("/admin/deleteallorders")
    public String deleteallorders(Model model){
        Integer integer = orderService.deleteAllOrders();
        if (integer!=null){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除失败");
        }
        return "forward:/admin/selectallorderpage";
    }

    @GetMapping("/admin/deleteallusers")
    public String deleteallusers(Model model){
        Integer integer = userService.deleteAllUsers();
        Integer integer1 = accountService.deleteAllAccounts();
        if (integer!=null&&integer1!=null){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除失败");
        }
        return "forward:/admin/getalluserpage";
    }

    @GetMapping("/admin/deleteallearns")
    public String deleteallearns(Model model){
        Integer integer = earnService.deleteAllEarn();
        if (integer!=null){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除失败");
        }
        return "forward:/admin/selectallearn";
    }
    @GetMapping("/admin/updateonegoodsstock")
    public String updateonegoodsstock(Integer id,Integer goodsStock ,Model model){
        Integer integer = goodsService.updateOneGoodsStock(id, goodsStock);
        if (integer!=null){
            model.addAttribute("msg","修改成功");
        }else {
            model.addAttribute("msg","修改失败");
        }
        return "forward:/admin/selectallgoodspage";
    }

}
